package saucaca.fragment;

import java.util.ArrayList;

import lemon.kenhnhacvn.BaseActivity;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;
import saucaca.Adapter.PlaylistOfflineAdapter;
import saucaca.CustomView.XListView;
import saucaca.Object.PlaylistObject;
import saucaca.Object.SongObject;
import saucaca.Util.TagName;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class SelectPlaylistDialogOffline extends DialogFragment {
	ArrayList<PlaylistObject> listPlaylist;
	SongObject song;
	ViewGroup rootView;
	XListView mView_listView;
	PlaylistOfflineAdapter adapter;
	private ProgressBar progressBar;
	ImageView imgEmpty, imgError;
	TextView txtStatus;
	private View mView_EmptyLoading = null;
	Button buttonCreate;
	 
	public static SelectPlaylistDialogOffline newInstance(SongObject song) {
		TagName.showLogError("Addplaylist", song.getTXNAME());
		SelectPlaylistDialogOffline f = new SelectPlaylistDialogOffline();
		Bundle args = new Bundle();
		args.putSerializable(MainActivity.ID, song);
		f.setArguments(args);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			song = (SongObject) getArguments().getSerializable(MainActivity.ID);
		} else {
			TagName.showToastShort(getActivity(), "Arguments nullll!!!!!!!!!!!!!");
		}
		initViewsSetting();
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle(R.string.string_playlist).setView(rootView).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dismiss();
			}
		}).create();
		return dialog;
	}

	private void initViewsSetting() {
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		rootView = (ViewGroup) inflater.inflate(R.layout.playlist_show, null, false);
		mView_listView = (XListView) rootView.findViewById(R.id.layoutlistview_listview);
		mView_listView.setPullLoadEnable(false);
		adapter=new PlaylistOfflineAdapter(getActivity());
		mView_listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ArrayList<SongObject> musiclist = new ArrayList<SongObject>();
				musiclist.add(song);
				try {
					LocalAllMusic.db.addMusicToPlayList(musiclist, adapter.getItem(position));
					BaseActivity.initMediaData();
					dismiss();
				} catch (Exception e) {
				}
			}
		});
		mView_listView.setPullLoadEnable(false);
		mView_listView.setAdapter(adapter);
		mView_EmptyLoading = rootView.findViewById(R.id.empty_loading);
		mView_listView.setEmptyView(mView_EmptyLoading);
		mView_listView.startProgressBar();
		
		progressBar = (ProgressBar) mView_EmptyLoading.findViewById(R.id.empty_loading_progress);
		imgEmpty = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_NoItem);
		imgError = (ImageView) mView_EmptyLoading.findViewById(R.id.empty_loading_Error);
		txtStatus = (TextView) mView_EmptyLoading.findViewById(R.id.empty_loading_text);
		buttonCreate = (Button) rootView.findViewById(R.id.btn_footer);
		if(adapter.getlist().size()==0){
			mView_listView.stopProgressBar();
			progressBar.setVisibility(View.GONE);
		}
		buttonCreate.setText("Create New Playlist");
		buttonCreate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				newPlayListDialog();
			}
		});
	}

	private void newPlayListDialog() {
		AlertDialog.Builder builder = new Builder(getActivity());
		builder.setTitle("Tạo Playlist");
		final EditText tv = new EditText(getActivity());
		tv.setHint("Playlist name");
		builder.setView(tv);
		builder.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				String namePlaylist = tv.getText().toString();
				if (namePlaylist != null) {
					LocalAllMusic.db.creatNewPlayList(namePlaylist);
					BaseActivity.initMediaData();
					adapter.notifyDataSetChanged();
				}
			}
		});
		builder.setNegativeButton("Cancel", new android.content.DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
}
